package com.ocr;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.ocr.*;
import com.entity.HistoryItem;
import com.util.OCRCostant;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class ResultActivity extends Activity {
	private EditText txtResult;
	private Button btnCopy;
	private Button btnBack;
	private ImageButton btnSave;
	private ImageButton btnShare;
	private String  rsText="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		txtResult = (EditText) findViewById(R.id.txt_result);
		btnCopy = (Button) findViewById(R.id.btn_copy);
		btnSave = (ImageButton) findViewById(R.id.btn_save);
		btnShare = (ImageButton) findViewById(R.id.btn_share);
		txtResult.setMovementMethod(new ScrollingMovementMethod());
		/*
		 * get extra when click from item
		 */
		if (getIntent().getExtras() != null) {
			if (getIntent().getExtras().get(OCRCostant.ITENT_HISTROY_TO_RESULT) != null)
				rsText=(getIntent().getExtras()
						.get(OCRCostant.ITENT_HISTROY_TO_RESULT).toString());
			else
				rsText=ImageActivity.RESULT_TEXT;
		} else
			rsText=ImageActivity.RESULT_TEXT;
		txtResult.setText(rsText);
		btnBack = (Button) findViewById(R.id.btn_back);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		btnCopy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
				ClipData clip = ClipData.newPlainText("label",
						txtResult.getText());
				clipboard.setPrimaryClip(clip);
			}
		});
		btnSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					/*
					 * save file with name is now time
					 */
					Date date = new Date();
					DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
					String fileName = "/ocr-result/" + dateFormat.format(date)
							+ ".docx";
					File myFile = new File(Environment
							.getExternalStorageDirectory() + fileName);
					myFile.createNewFile();
					FileOutputStream fOut = new FileOutputStream(myFile);
					OutputStreamWriter myOutWriter = new OutputStreamWriter(
							fOut);
					myOutWriter.append(txtResult.getText());
					myOutWriter.close();
					fOut.close();
					Toast.makeText(
							getBaseContext(),
							getResources().getString(
									R.string.message_save_success)
									+ fileName, Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					Toast.makeText(getBaseContext(), e.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			}

		});
		btnShare.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent sharingIntent = new Intent(
						android.content.Intent.ACTION_SEND);
				sharingIntent.setType("text/plain");
				String rsText=txtResult.getText().toString();
				sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
						rsText);
				startActivity(Intent.createChooser(sharingIntent,getResources().getString(R.string.share_via)));
			}
		});
	}

}
